package com.sltx.controller;


import com.sltx.controller.base.BaseController;
import com.sltx.dao.aaa.PackageMapper;
import com.sltx.dto.aa;
import com.sltx.service.PackageService;
import com.sltx.util.ExcelUtil;
import com.sltx.util.ImportExcel;
import com.sltx.util.PageData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;


@Controller
@RequestMapping("/Package")
public class PackageController extends BaseController {

    @Autowired
    private PackageService packageService;

    @Autowired
    private PackageMapper packageMapper;

    //将Excel表格数据导入到数据库
    @RequestMapping("/import")
    @ResponseBody
    public Object getEntNameNum(@RequestParam MultipartFile file) {
        PageData pd = this.getPageData();
        try {
            String fileName = file.getOriginalFilename();
            InputStream inputStream = file.getInputStream();
            List<Map<String, Object>> list = ImportExcel.readExcel(fileName, inputStream);
            return packageService.insertExcel(list, pd);
        } catch (Exception e) {
            return e;
        }
    }

    //将Excel表格数据导入到企业包的数据更新包名和用户名
    @RequestMapping("/updateExcel")
    @ResponseBody
    public Object updateExcel() {
        PageData pd = this.getPageData();
        List<Map<String, Object>> num = packageMapper.getPackageName(pd);
        if (num.size() > 0) {
            this.deleteExcel();
            return "企业包名重复";
        }
        if (StringUtils.isEmpty(pd.get("qymdmc"))) {
            String fileName[] = pd.get("excelName").toString().split(".xlsx");
            pd.put("qymdmc", fileName[0]);
            List<Map<String, Object>> num1 = packageMapper.getPackageName(pd);
            if (num1.size() > 0) {
                this.deleteExcel();
                return "企业包名重复";
            }
        }
        int m = packageService.updateExcel(pd);
        int m2 = packageService.getQynum(pd);
        if (m > 0 && m2 > 0) {
            return m2;
        }
        return "添加失败";
    }

    //将Excel表格数据导入到数据库的数据删了
    @RequestMapping("/deleteExcel")
    @ResponseBody
    public Object deleteExcel() {
        PageData pd = this.getPageData();
        return packageService.deleteExcel(pd);
    }

    //Excel表格下载模板
    @RequestMapping("/getExcelTem")
    @ResponseBody
    public String getExcelTem(HttpServletResponse response) {
        // 存储Excel表格列名
        List<String> rowNameList = Arrays.asList(
                "企业名称",
                "统一社会信用代码");
        // 存储Excel表格内容，容器
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        String qymc = "腾讯(示例)";
        String shtydm = "123456(示例)";
        map.put("企业名称", qymc);
        map.put("统一社会信用代码", shtydm);
        list.add(map);
        try {
            ExcelUtil.excelPort("股权穿透-模板", rowNameList, list, response);
            return "下载成功";
        } catch (Exception e) {
            return "下载失败";
        }
    }

    //获取所有企业包
    @RequestMapping("/getPackage")
    @ResponseBody
    public Map getPackage() {
        PageData pd = this.getPageData();
        if (pd.get("tjdata").equals("添加时间")) {
            pd.put("tjdata", "");
        }
        return packageService.getPackage(pd);
    }

    //修改企业包名称
    @RequestMapping("/Update")
    @ResponseBody
    public String Update() {
        PageData pd = this.getPageData();
        List<Map<String, Object>> num = packageMapper.getPackageName(pd);
        if (num.size() > 0) {
            return "企业包名重复";
        }
        int mm = packageService.Update(pd);
        if (mm > 0) {
            return "修改成功";
        }
        return "修改失败";
    }

    //删除企业包
    @RequestMapping("/getDeletePackage")
    @ResponseBody
    public String DeletePackage() {
        PageData pd = this.getPageData();
        return packageService.DeletePackage(pd);
    }

    //Excel表格下载
    @RequestMapping("/getExcel")
    @ResponseBody
    public String getExcel(HttpServletResponse response) {
        PageData pd = this.getPageData();
        // 存储Excel表格列名
        List<String> rowNameList = Arrays.asList(
                "企业名称",
                "统一社会信用代码");
        // 存储Excel表格内容，容器
        List<Map<String, String>> list = new ArrayList<>();
        List<Map<String, Object>> result = packageService.getExcel(pd);
        for (Map m : result) {
            Map<String, String> map = new HashMap<>();
            String entname = m.get("entname").toString();
            String uniscid = m.get("uniscid").toString();
            map.put("企业名称", entname);
            map.put("统一社会信用代码", uniscid);
            list.add(map);
        }
        try {
            ExcelUtil.excelPort(pd.get("qymdmc").toString(), rowNameList, list, response);
            return "下载成功";
        } catch (Exception e) {
            return "下载失败";
        }
    }

    //获取企业包中的企业
    @RequestMapping("/getPackageDetails")
    @ResponseBody
    public List<Map<String, Object>> getPackageDetails() {
        PageData pd = this.getPageData();
        List<Map<String, Object>> result = packageService.getPackageDetails(pd);
        return result;
    }

    //删除企业包中企业
    @RequestMapping("/getDeleteQymc")
    @ResponseBody
    public String DeleteQymc() {
        PageData pd = this.getPageData();
        int mm = packageService.DeleteQymc(pd);
        int num = packageService.getCountEnterprise(pd);
        pd.put("num", num);
        packageService.updatePackage(pd);
        String result = "";
        if (mm > 0) {
            result = "删除成功";
        } else {
            result = "删除失败";
        }
        return result;
    }

//    //企业包中添加企业
//    @PostMapping("/addQymc")
//    @ResponseBody
//    public String addQymc(@RequestBody List<EntcolDTO> entcolDTO) {
//        PageData pd = this.getPageData();
//        int a = packageService.addQymdmcId(pd, entcolDTO);
//        int num = packageService.getCountEnterprise(pd);
//        pd.put("num", num);
//        packageService.updatePackage(pd);
//        String result = "";
//        if (a > 0) {
//            result = "添加成功";
//        } else {
//            result = "添加失败";
//        }
//        return result;
//    }

    //企业包中添加企业
    @ApiOperation("企业包中添加企业")
    @RequestMapping(value = "/addQymc2", method = RequestMethod.POST)
    @ResponseBody
    public String addQymc2(@RequestBody aa aa) {
        PageData pd = this.getPageData();
        String qymdmc = aa.getQymdmc();
        String user = aa.getUser();
        pd.put("qymdmc", qymdmc);
        pd.put("user", user);
        int a = packageService.addQymdmcId2(aa);
        int num = packageService.getCountEnterprise(pd);
        pd.put("num", num);
        packageService.updatePackage(pd);
        String result = "";
        if (a > 0) {
            result = "添加成功";
        } else {
            result = "添加失败";
        }
        return result;
    }

}
